import 'package:animated_transitions/animated_transitions.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:vamos/view/main/main_page.dart';
import '../../core/di/service_locator.dart';

/// 应用启动页面
/// 显示背景图片和logo，3秒后自动跳转到登录页面
class AppLaunchScreen extends StatefulWidget {
  const AppLaunchScreen({super.key});

  @override
  State<AppLaunchScreen> createState() => _AppLaunchScreenState();
}

class _AppLaunchScreenState extends State<AppLaunchScreen> {
  @override
  void initState() {
    super.initState();
    // 初始化服务
    _initializeServices();
  }

  /// 初始化所有服务
  void _initializeServices() async {
    try {
      await initializeAllServices();
      if (mounted) {
        // 延迟3秒后跳转到主界面
        _proceedToMainScreen();
      }
    } catch (e) {
      // 如果初始化失败，仍然跳转（可以显示错误提示）
      if (mounted) {
        _proceedToMainScreen();
      }
    }
  }

  /// 跳转到主界面
  void _proceedToMainScreen() {
    Future.delayed(const Duration(seconds: 3), () {
      if (mounted) {
        Navigator.of(context).pushReplacement(
          TransitionPageRoute(
            builder:
                (context) => StoreProvider<AppState>(
                  store: appStore,
                  child: const VamosMainInterface(),
                ),
            transitionAnimation: GrowingBarsTransition(
              colors: [Color(0xFF83BE4A), Color(0xFFF3E3C6), Color(0xFFFB894E)],
            ),
          ),
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/my_background.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Logo容器
              Container(
                width: 98.w,
                height: 98.w,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(34.r),
                  image: const DecorationImage(
                    image: AssetImage('assets/images/logo.png'),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              // 添加间距
              SizedBox(height: 40.h),
              // Loading动画
              LoadingAnimationWidget.fourRotatingDots(
                color: Colors.white,
                size: 50.w,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
